package com.esheeper.logictimestampprovider.controller;

import com.esheeper.logictimestampprovider.util.ResponseUtil;
import com.esheeper.logictimestampprovider.util.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
public class LogicTimestampController {

    @Autowired
    ResponseUtil responseUtil;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    // 在方法里调用执行
    // 第一个参数 对lua 脚本对象的调用
    // 第二个参数 是 lua 脚本中 Keys 参数
    // 第三个参数  是 lua  脚本中 ARGV 参数

    @Resource
    DefaultRedisScript<Long> deductStockRedisScript;
    @GetMapping("/{key}/setTimestamp")
    Map setLogicTimestamp(@PathVariable("key") String key,
                          @RequestParam("token") String token,
                          @RequestParam("randomString") String randomString) throws NoSuchAlgorithmException {

        if(ValidatorUtil.validateToken(token, randomString) == false){
            responseUtil.fail(ResponseUtil.REQUEST_ERROR_NOT_AUTHORIZATION,null);
        }

        if(key != "forum" && key != "reply") responseUtil.fail(ResponseUtil.REQUEST_ERROR_NOT_FIND_RESOURCE,null);
        Long result = stringRedisTemplate.execute(deductStockRedisScript, Collections.singletonList(key));
        System.out.println(result);
        return responseUtil.success(result);
    }

//     测试接口
//    @GetMapping("/{key}")
//    Map test(@PathVariable String key){
//        if(key != "forum" && key != "reply") responseUtil.fail(ResponseUtil.REQUEST_ERROR_NOT_FIND_RESOURCE,"key");
//
//        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
//        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/Atomic.lua")));
//        redisScript.setResultType(Long.class);
//        System.out.println(redisScript.getSha1());
//        Long result = stringRedisTemplate.execute(redisScript, Collections.singletonList(key),"UUID");
//        System.out.println(result);
//        return responseUtil.success(result);
//    }

    @GetMapping("/{key}/getTimestamp")
    Map getLogicTimestamp(@PathVariable("key") String key,
                          @RequestParam("token") String token,
                          @RequestParam("randomString") String randomString) throws NoSuchAlgorithmException {

        if(ValidatorUtil.validateToken(token, randomString) == false){
            responseUtil.fail(ResponseUtil.REQUEST_ERROR_NOT_AUTHORIZATION,null);
        }

        if(key != "forum" && key != "reply") responseUtil.fail(ResponseUtil.REQUEST_ERROR_NOT_FIND_RESOURCE,null);
        Integer number = Integer.valueOf(stringRedisTemplate.opsForValue().get(key));
        return responseUtil.success(number);
    }

}
